package com.topgay.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;

import com.topgay.domain.ValueStrings;

public class LoginCheckInterceptor  implements HandlerInterceptor{

	@Override
	//view 처리 종료 후에 호출
	//3. Object - Handler 메소드(HandlerMethod)
	//4. Exception - 발생한 오류
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception e)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler, ModelAndView mv) throws Exception {
//		System.out.println("[postHandle]");
		response.setHeader("cache-control", "no-store, no-cache");
		
	}

	@Override
	//Handler처리 전에 호출
	//return boolean : false 리턴 -> 다음으로 이동하지 않는다.
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler) throws Exception {
		System.out.println("[preHandle]");
		HttpSession session = request.getSession();
		Object loginAdminInfo = session.getAttribute("login_info");
		if(loginAdminInfo == null){
//			ModelAndView mv = new ModelAndView("/admin/body/login.jsp", "error_msg", ValueStrings.getInstance().getErrorMsgLogin());
			ModelAndView mv = new ModelAndView("redirect:/index.do", "error_msg", ValueStrings.getInstance().getErrorMsgLogin());
			throw new ModelAndViewDefiningException(mv);
		}
		return true;
	}
	
}
